#include "Rand.h"
#include <windows.h>
#include <time.h>

CRand::CRand(void)
{
	Srand() ; 
}

CRand::~CRand(void)
{
}

void CRand::Srand()
{
	DWORD dwCPUTickCount;
	__asm
	{
		// Save registers.
		push	eax
			push	ebx
			push	edx

			// Get the tick count.
			rdtsc
			lea		ebx,dwCPUTickCount
			mov		[ebx],eax
			//mov		[ebx+4],edx

			// Restore registers.
			pop		edx
			pop		ebx
			pop		eax
	}
	DWORD dwSeed=time(NULL)*GetTickCount()*dwCPUTickCount;

	holdrand= dwSeed ;
}

int CRand::rand()
{
	return (((holdrand = holdrand * 0x015a4e35L + 1L) >> 16) & 0x7fff);
}

int CRand::rand(int nLen )
{
	if( 0 == nLen ) return 0 ; 

	return rand() % nLen ; 
}
